<?php

/**
 * 品牌品牌
 */

namespace app\admin\service;

use think\Service;
use app\admin\model\ProductBrand as BrandModel;

class Brand extends BaseService
{

    /**
     * 获取品牌列表
     * @param $param
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($param = [], $page = 1, $limit = 10)
    {
        $where = [['is_del', '=', 0]];
        $arrData = ['code' => 0, 'message' => 'success'];
        if ($param['name']) {
            $where[] = ['name', 'like', '%' . $param['name'] . '%'];
        }

        $oBrandModel = BrandModel::where($where);
        $total = $oBrandModel->count();
        $result = $oBrandModel->with(['get_creator_name', 'get_update_name'])->page($page, $limit)->order('sort_order desc,id desc')->select();
        $arrData['data'] = ['list' => $result->toArray(), 'total' => $total];
        return $arrData;
    }

    /**
     * 删除单条或多条品牌数据
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete($param)
    {
        $where = [['is_del', '=', '0'], ['id', 'in', $param['ids']]];
        $oBrandModel = BrandModel::where($where);
        $brand = $oBrandModel->find();
        if (!$brand) {
            return ['code' => 1, 'message' => '获取品牌信息失败！'];
        }
        $result = $oBrandModel->update(['is_del' => time()]);
        if ($result) {
            $this->clearCTerminalCache(6); // 清除C端缓存
            $this->logger->logOperation($param['ids'], 'brand', '删除', '删除品牌');
            return ['code' => 0, 'message' => '删除品牌成功！'];
        } else {
            return ['code' => 1, 'message' => '删除品牌失败！'];
        }
    }

    /**
     * 添加修改品牌
     * @param $param
     * @return array|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save($param)
    {
        $checkBrandModel = BrandModel::where([['name', '=', $param['name']], ['is_del', '=', 0]]);
        if ($param['id']) {
            $checkBrandModel->where('id', '<>', $param['id']);
        }
        if ($checkBrandModel->find()) {
            return ['code' => 1, 'message' => '品牌名称重复！'];
        }

        if ($param['id']) {
            $oBrandModel = BrandModel::find($param['id']);
            $oBrandModel->updater = $param['operator_id'];
            $oBrandModel->update_time = date('Y-m-d H:i:s', time());
        } else {
            $oBrandModel = new BrandModel();
            $oBrandModel->creator = $param['operator_id'];
            $oBrandModel->create_time = date('Y-m-d H:i:s', time());
            $oBrandModel->is_del = 0;
        }

        $oBrandModel->name = $param['name'];
        $oBrandModel->image = $param['image'];
        $oBrandModel->sort_order = $param['sort_order'];
        $oBrandModel->firmware_url = $param['firmware_url'];
        $oBrandModel->description = $param['description'];

        if ($oBrandModel->save()) {
            $this->logger->logOperation($oBrandModel->id, 'brand', '添加/修改', '保存品牌');
            $this->clearCTerminalCache(6); // 清除C端缓存
            return ['code' => 0, 'message' => '保存品牌成功！'];
        }
    }

    /**
     * 获取单个品牌信息
     * @param int $id 品牌ID
     * @return array
     */
    public function getInfo($param)
    {
        $where = [['is_del', '=', '0'], ['id', '=', $param['id']]];
        $oBrandModel = BrandModel::where($where)->with(['get_creator_name', 'get_update_name'])->find();
        if (!$oBrandModel) {
            return ['code' => 1, 'message' => '获取品牌信息失败！'];
        }
        return ['code' => 0, 'message' => 'success', 'data' => $oBrandModel];
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSelect()
    {
        $where = [['is_del', '=', 0]];
        $arrData = ['code' => 0, 'message' => 'success'];

        $oCategoryModel = BrandModel::where($where);
        $total = $oCategoryModel->count();
        $result = $oCategoryModel->field('id AS value,name as label')->select();


        $arrData['data'] = ['list' => $result->toArray(), 'total' => $total];
        return $arrData;
    }
}
